package com.baozun.houji.order.adapter

import com.baozhun.mall.common.base.BaseListAdapter
import com.baozun.houji.order.BR
import com.baozun.houji.order.R
import com.baozun.houji.order.databinding.ItemOrderListBinding
import com.baozhun.mall.common.model.bean.OrderInfoBean
import com.baozhun.mall.common.model.enums.OrderStatus
import com.baozun.houji.order.util.order.OrderActionManager
import com.baozun.houji.order.util.order.OrderActionFinishListener
import com.baozun.houji.order.util.order.enums.OrderSource
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @Title：订单列表
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
class OrderListAdapter(data: MutableList<OrderInfoBean>, var actionResultListener: OrderActionFinishListener? = null) : BaseListAdapter<OrderInfoBean, ItemOrderListBinding>(
    R.layout.item_order_list, data
) {
    override fun convert(holder: BaseDataBindingHolder<ItemOrderListBinding>, item: OrderInfoBean) {
        super.convert(holder, item)
        holder.dataBinding?.setVariable(BR.model, item)
        holder.dataBinding?.setVariable(BR.isRefundOrderList, item.order_status_name.isNullOrEmpty())
        OrderActionManager.getInstance(
            item.order_no,
            item.after_sale_no,
            item.order_status,
            item.after_sale_status,
            orderSource = if (item.order_status_name.isNullOrEmpty()) OrderSource.REFUND_lIST else OrderSource.ORLDER_lIST,
            amount = item.pay_amount ?: "",
            actionResultListener = actionResultListener
        ).addActionBt(holder.dataBinding?.llBottomBt)
        holder.dataBinding?.executePendingBindings()
    }

    /**
     * 更新支付剩余时间
     */
    fun updatePaySurplusTime() {
        for (item in data) {
            item.pay_expire_time -= 1
            //若有item倒计时完成则刷新数据
            if (item.order_status == OrderStatus.WAIT_DELIVERY.status && item.pay_expire_time <= 0) {
                actionResultListener?.reload()
                break
            }
        }
        notifyDataSetChanged()
    }
}